import { plugin } from "alemon";
import data from '../../model/fantasyData.js';
import {
  __PATH,
  Add_HP,
  Read_player,
  existplayer,
  Read_equipment,
  Write_equipment,
  Write_player,
  Read_bag,
  Write_bag,
  Read_mapevent,
  Read_mapmove,
  Set_battleobject,
  Set_battleplayer,
  Judge_up,
  Add_bag,
  Read_state,
  Write_state,
  Read_battleobject,
  Read_battleplayer,
  Write_battleobject,
  Write_battleplayer,
  Read_thingtype,
  ifmingzhong,
  ifbaoji,
  baoji,
  battle_round,
  battle_add,
  delete_battleplayer,
  delete_battleobject,
  Read_skill,
  Read_proplist,
  Read_monster,
  Write_team,
  Read_team,
  object_time,
  sleep,
  Read_teamIndex,
  Read_makelist,
  Num_bag,
  Read_decomposelist,

} from '../../model/fantasy.js';
import { basename } from "path/win32";
import { userInfo } from "node:os";
import { REPL_MODE_SLOPPY } from "node:repl";


export class Makething extends plugin {
  constructor() {
    super({
      priority: 600,
      rule: [
                {
                    reg: /^合成列表.*$/,
                    fnc: 'make_list'
                },
                {
                    reg: /^合成信息.*$/,
                    fnc: 'make_information'
                },
                {
                    reg: /^合成物品.*$/,
                    fnc: 'make_thing'
                },
                {
                    reg: /^分解物品.*$/,
                    fnc: 'decompose_thing'
                },
                
      ],
    });
    
  }

    //查看合成列表
    async make_list(e){
    e.cmd_msg = e.cmd_msg.replace("/", "");
    let usr_qq = e.user_id;
    //有无存档
    let ifexistplay = await existplayer(usr_qq);
    if (!ifexistplay) {
      return;
    }
    if(await Read_state(usr_qq) == "组队战斗"){
      return;
    }
    if(await Read_state(usr_qq) == "战斗"){
      return;
    }
    let player = await Read_player(usr_qq);
    let makelist = await Read_makelist(usr_qq);
    let name0 = e.cmd_msg.replace(/^合成列表/,'');
    let name = name0.replace(/\d+/g, "");
    let match = name0.match(/\d+$/);
    let num = match ? match[0] : 1;
    let reallist = "";
    if(name == null || name == ""){
      reallist = makelist;
    }else if(name.includes("搜索") == false){
      reallist = makelist.filter(obj => obj["类型"] == name);
      if(reallist == undefined){
        e.reply(`【${player.昵称} Lv.${player.等级}】\n————————————\n不存在该类型物品！`);
        return;
      }
    }else if(name.includes("搜索")){
      name = name.replace("搜索", "");
      reallist = makelist.filter(obj => obj["名称"].includes(name) == true);
      if(reallist == undefined){
        e.reply(`【${player.昵称} Lv.${player.等级}】\n————————————\n不存在该关键词物品！`);
        return;
      }
    }
      let len = reallist.length;
      let len1 = Math.floor((len - 1)/ 10) + 1;
      if(num > len1){
        num = len1;
      }
      let word = `======${name}合成列表======\n————————————`;
      if(len == 0){
        e.reply(`======合成列表======\n————————————\n[空空如也]\n————————————\n【第1页，共1页】\nTips:发送[合成列表+页码]查看列表\nTips:发送[合成列表+类型+页码]查看该类型列表\nTips:发送[合成列表+搜索+物品关键词+页码]查看关键词列表\nTips:发送[合成物品+名称+配方号+"*"+数量]合成物品`);
        return;
      }
      let word0 = "";
      let i = 10 * (num - 1);
      let b = i + 10;
      if(num == len1){
        b = len;
      }
      for(i; i < b; i++){
      word0 = `\n[${reallist[i]["名称"]}]`;
      word += word0;
      }
      e.reply(word + `\n————————————\n【第${num}页，共${len1}页】\nTips:发送[合成列表+页码]查看列表\nTips:发送[合成列表+类型+页码]查看该类型列表\nTips:发送[合成列表+搜索+物品关键词+页码]查看关键词列表\nTips:发送[合成物品+名称+配方号+"*"+数量]合成物品`);
      return;
    }

    //查看合成信息
    async make_information(e){
    e.cmd_msg = e.cmd_msg.replace("/", "");
    let usr_qq = e.user_id;
    //有无存档
    let ifexistplay = await existplayer(usr_qq);
    if (!ifexistplay) {
      return;
    }
    if(await Read_state(usr_qq) == "组队战斗"){
      return;
    }
    if(await Read_state(usr_qq) == "战斗"){
      return;
    }
    let player = await Read_player(usr_qq);
    let makelist = await Read_makelist(usr_qq);
    let name0 = e.cmd_msg.replace("合成信息","");
    let reallist = makelist.find(obj => obj["名称"] == name0);
    if(reallist == undefined){
      e.reply(`【${player.昵称} Lv.${player.等级}】\n————————————\n不存在该物品！`);
      return;
    }
    let len1 = reallist["材料"].length;
    let len2 = "";
    let word1 = "";
    for(let a = 0; a < len1; a++){
      word1 += `\n——————————\n【配方[${a + 1}]】`;
      len2 = reallist["材料"][a]["配方"].length;
      for(let b = 0; b < len2; b++){
        word1 += `\n   [${reallist["材料"][a]["配方"][b]["名称"]}] * ${reallist["材料"][a]["配方"][b]["数量"]}`;
      }
    }
    e.reply(`====合成信息====\n【${name0}】` + word1);
    return;
    }

    //合成物品
    async make_thing(e){
    e.cmd_msg = e.cmd_msg.replace("/", "");
    let usr_qq = e.user_id;
    //有无存档
    let ifexistplay = await existplayer(usr_qq);
    if (!ifexistplay) {
      return;
    }
    if(await Read_state(usr_qq) == "组队战斗"){
      return;
    }
    if(await Read_state(usr_qq) == "战斗"){
      return;
    }
    let player = await Read_player(usr_qq);
    let makelist = await Read_makelist(usr_qq);
    let name0 = e.cmd_msg.replace(/^合成物品/,'');
    let name = name0.replace(/[\d*]/g, "");
    let match = name0.replace(/[^\d*]/g, "");
    let num1 = match.split("*")[0] ? match.split("*")[0] : 1;
    let num2 = match.split("*")[1] ? match.split("*")[1] : 1;
    if(name == null || name == ""){
      e.reply(`【${player.昵称} Lv.${player.等级}】\n————————————\n请输入正确的物品名！`);
      return;
    }else if(makelist.find(obj => obj["名称"] == name) == undefined){
      e.reply(`【${player.昵称} Lv.${player.等级}】\n————————————\n该物品无法合成！`);
      return;
    }else if((num2 > 0) == false || Number.isInteger(num2)){
      e.reply(`【${player.昵称} Lv.${player.等级}】\n————————————\n合成数量要为正整数！`);
      return;
    }
    let makathing = makelist.find(obj => obj["名称"] == name);
    let len0 = makathing["材料"].length;
    if(num1 > len0 || Number.isInteger(num1) || num1 < 1){
      e.reply(`【${player.昵称} Lv.${player.等级}】\n————————————\n不存在此配方！\n此物品存在[${len0}]种配方`);
      return;
    }
    let materail = makathing["材料"][num1 - 1]["配方"];
    let len1 = materail.length;
    let num3 = 0;
    let num4 = 0;
    let a = 0;
    for(a = 0; a < len1; a++){
      num3 = await Num_bag(usr_qq, materail[a]["名称"]);
      num4 = materail[a]["数量"] * num2;
      if(num3 < num4){
        e.reply(`【${player.昵称} Lv.${player.等级}】\n————————————\n你的物品不足！\n需要[${materail[a]["名称"]}] (${num3}/${num4})个`);
        return;
      }
    }
    let word = `【${player.昵称} Lv.${player.等级}】\n————————————\n合成成功！`
    for(a = 0; a < len1; a++){
      num4 = materail[a]["数量"] * num2;
      await Add_bag(usr_qq, materail[a]["名称"], -num4);
      word += `\n消耗[${materail[a]["名称"]}] * ${num4}`
    }
    await Add_bag(usr_qq, name, num2);
    word += `\n获得[${name}] * ${num2}`
    e.reply(word);
    return;
    }


    //分解物品
    async decompose_thing(e){
    e.cmd_msg = e.cmd_msg.replace("/", "");
    let usr_qq = e.user_id;
    //有无存档
    let ifexistplay = await existplayer(usr_qq);
    if (!ifexistplay) {
      return;
    }
    if(await Read_state(usr_qq) == "组队战斗"){
      return;
    }
    if(await Read_state(usr_qq) == "战斗"){
      return;
    }
    let player = await Read_player(usr_qq);
    let decomposelist = await Read_decomposelist(usr_qq);
    let name0 = e.cmd_msg.replace(/^分解物品/,'');
    let name = name0.split("*")[0];
    let num = name0.split("*")[1] ? name0.split("*")[1] : 1;
    if((num > 0) == false || Number.isInteger(num)){
      e.reply(`【${player.昵称} Lv.${player.等级}】\n————————————\n分解数量要为正整数！`);
      return;
    }
    let bagnum = await Num_bag(usr_qq, name);
    if(num > bagnum){
      e.reply(`【${player.昵称} Lv.${player.等级}】\n————————————\n你的该物品不足！`);
      return;
    }
    let thing = decomposelist.find(obj => obj["名称"] == name);
    if(thing == undefined){
      e.reply(`【${player.昵称} Lv.${player.等级}】\n————————————\n该物品无法分解！`);
      return;
    }
    let finish = [];
    let len1 = thing["材料"].length;
    let random1 = 0;
    let index1;
    let a;
    let b;
    for(a = 0; a < num ;a++){
      for(b = 0; b < len1; b++){
        random1 = Math.floor(100 * Math.random());
        if(random1 <= thing["材料"][b]["概率"]){
            index1 = finish.findIndex(obj => obj["名称"] == thing["材料"][b]["名称"]);
            if(index1 == -1){
              finish.push({"名称": thing["材料"][b]["名称"], "数量": thing["材料"][b]["数量"]});
            }else if(index1 >= 0){
              finish[index1]["数量"] = Number(finish[index1]["数量"]) + Number(thing["材料"][b]["数量"]);
            }
        }
      }
    }
    let len2 = finish.length;
    let word1 = `【${player.昵称} Lv.${player.等级}】\n————————————\n分解成功！\n消耗[${name}] * ${num}`
    await Add_bag(usr_qq, name, -num);
    for(let c = 0; c < len2; c++){
      await Add_bag(usr_qq, finish[c]["名称"], finish[c]["数量"]);
      word1 += `\n获得[${finish[c]["名称"]}] * ${finish[c]["数量"]}`;
    }
    e.reply(word1);
    return;
    }


}